package com.tairanchina.md.meeting.resource;

import com.alibaba.fastjson.JSON;
import com.tairanchina.md.constants.CommonConstants.ErrorCode;
import com.tairanchina.md.elife.rabbit.constants.ConnConstants;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.constants.MeetingProvider;
import com.tairanchina.md.meeting.dto.AwarderDto;
import com.tairanchina.md.turntable.dto.YearMeetingLotteryDto;
import com.tairanchina.txRestFilter.engine.jersey.TxJerseyTools;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.ws.rs.*;
import javax.ws.rs.core.Response;

/**
 * 中奖人模块
 */
@Path(MeetingConstants.PathRoute.PRIZE_PERSON_ROOT)
public class PrizePersonResource
{
    Logger logger = LoggerFactory.getLogger(PrizePersonResource.class);
    @DELETE
    public Response cancel(@FormParam("userPhone") String phone)
    {
        //参数计划是手机号
        if(StringUtils.isBlank(phone)){
            return TxJerseyTools.returnAbort(ErrorCode.ERROR_ILLEGAL_PARAMTER);
        }
        /**
         * 类实现描述：TODO 取消中奖人资格, 优先级可调低
         * hzyjie 16/12/24 12:48
         */
        try {
            //删除中奖人信息
            int awardLevel = MeetingProvider.awarderService.cancleAwarder(phone);


            //发送取消中奖人到阿里云
            if(awardLevel != 0){
                YearMeetingLotteryDto lotteryDto = new YearMeetingLotteryDto();
                lotteryDto.setUserPhone(phone);
                lotteryDto.setAwardLevel(awardLevel);
                lotteryDto.setAwardState(false);
                ConnConstants.getProducerClient().sendMessage(MeetingConstants.EXCHANGE_NAME_YEAR_LOTTERY,MeetingConstants.QUEUE_KEY_YEAR_LOTTERY,lotteryDto);

            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("取消中奖人资格失败。e:{}",e);
        }

        return TxJerseyTools.returnSuccess();
    }

    @GET
    public Response personList(@QueryParam("awardLevel") Integer awardLevel)
    {
        /**
         * 类实现描述：TODO 返回中奖人列表
         * hzyjie 16/12/24 12:48
         */
        AwarderDto awarderDto = new AwarderDto();
        try {

            awarderDto = MeetingProvider.awarderService.getAwarderList(awardLevel);

        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询中奖人列表失败，e:{}",e);
            awarderDto.setAwarderNum(0);
        }

        return TxJerseyTools.returnSuccess(JSON.toJSONString(awarderDto));
    }
}
